<!-- 更多党建智搜 -->
<template>
  <div>
    <van-nav-bar
      :title="`更多${fourSearch[index].text}搜索`"
      left-arrow
      @click-left="$router.push('/search')"
    />
    
    <van-list
      v-if="fourSearch[index].array.length>0"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <div @click="resultTurn(ele)" class="personLi" v-for="(ele,i) in fourSearch[index].array" :key="i">
        <span v-if="index==0" v-html="brightenKeyword(ele.nickname, data.keyWord)"></span>
        <div v-if="index==1" class="ellipsis" v-html="brightenKeyword(ele[fourSearch[index].arrayFeild], data.keyWord)"></div>
        <div v-if="index==2||index==3" class="ellipsis" v-html="ele[fourSearch[index].arrayFeild]"></div>
      </div>
    </van-list>
  </div>
</template>

<script>
import { getUserInfo } from '@/api/index.js'
import { turnHref } from '@/untils/untils.js'
export default {
  data() {
    return {
      index:null,
      loading:true,
      finished:false,
      fourSearch:[
        {
          text:'人员',
          arrayFeild:'nickname',
          array:[]
        },
        {
          text:'组织',
          arrayFeild:'organname',
          array:[]
        },
        {
          text:'制度',
          arrayFeild:'title',
          array:[]
        },
        {
          text:'知识',
          arrayFeild:'title',
          array:[]
        },
      ],
      data:{
        "methodName":"quertNickNameLike",
        "serviceName":"PersonalSearch",
        "pageindex":1,                               
        "pagesize":"10",                           
        "usercode":"",                      
        "roleid":"" ,
        "keyWord":"",                                                 
      },
      lastTwoData:{
        "methodName":"queryEsDataH5",
        "serviceName":"SearchEngines",
        "currPage":0,                               
        "pageSize":"10",                           
        "empCode":"",                      
        "roleId":"" ,  
        "businessId":"970ae0834612451fbc5855bc0d126121",
        "keyWord":"",                                                 
       }
    };
  },
  created(){
    this.getRouteData()
    this.searchResult()
  },
  methods: {
    searchResult(){
      switch (this.index) {
        case '0':this.quertNickNameLike(); break;
        case '1':this.quertOrgNameLike(); break;
        case '2':this.queryEsDataH5(); break;
        case '3':this.queryEsDataH52(); break;
        default:
          break;
      } 
    },
    onLoad(){
      if (!this.finished) {
        this.loading = true
        this.data.pageindex = this.data.pageindex - 0 + 1
        this.lastTwoData.currPage = this.lastTwoData.currPage - 0 + 1
        this.searchResult()
      }
    },
    resultTurn(item){
      switch (this.index) {
        case '0':
          const setjson={"empCode":item.username};
          bmnative.fireEvent('AppJumpDy', JSON.stringify(setjson))
          break;
        case '1':
          this.$router.push({path:`/basicOragan/${item.organcode}`});
          break;
        case '2': case '3':
          this.goTourl(item)
          break;
        default:
          break;
      }
    },
    goTourl(item){
      const data = {
        methodName:'queryH5',
        serviceName:'Configure',
        roleId:this.data.roleid,
        empCode:this.data.usercode,
        url:item.url,
        uuid:item.uuid,
        businessId:item.businessId
      }
      getUserInfo(data,'DispatchAction','2').then(res => {
        if(res){
          res = res[0]
          if (res.terminal == 3) {
            turnHref(res.jumpaddress)
          } else if (res.terminal == 1) {
            const setjson={"parenttype":res.parenttype,"datauuid":res.datauuid};
            bmnative.fireEvent('AppJumpLink', JSON.stringify(setjson));
          }
        }
      })
    },
    getRouteData(){
      this.data.roleid = localStorage.getItem('roleId')
      this.data.usercode = localStorage.getItem('usercode')
      this.lastTwoData.roleId = localStorage.getItem('roleId')
      this.lastTwoData.empCode = localStorage.getItem('usercode')
      this.index = this.$route.params.index
      this.lastTwoData.keyWord = this.$route.params.keyWord
      this.data.keyWord = this.$route.params.keyWord
    },
    quertNickNameLike(){
      // 人员搜索
      this.data.methodName = 'quertNickNameLike'
      getUserInfo(this.data).then(res => {
        if (res.data) {
          if (res.data.length<10) {
            this.finished = true
          }
          this.fourSearch[0].array.push(...res.data)
          this.loading = false
        }
      })
    },
    quertOrgNameLike(){
       // 组织搜索
      this.data.methodName = 'quertOrgNameLike'
      getUserInfo(this.data).then(res => {
        if (res.data) {
          if (res.data.length<10) {
            this.finished = true
          }
          this.fourSearch[1].array.push(...res.data)
          this.loading = false
        }
      })
    },
    queryEsDataH5(){
      // 制度搜索
      this.lastTwoData.methodName = 'queryEsDataH5'
      getUserInfo(this.lastTwoData,'DispatchAction','2').then(res => {
        if (res) { console.log(res)
          if (res<10) {
            this.finished = true
          }
          this.fourSearch[2].array.push(...res)
          this.loading = false
        }
      })
    },
    queryEsDataH52(){
       // 知识搜索
      this.lastTwoData.methodName = 'queryEsDataH5'
      getUserInfo(this.lastTwoData,'DispatchAction','2').then(res => {
        if (res) {
          if (res<10) {
            this.finished = true
          }
          this.fourSearch[3].array.push(...res)
          this.loading = false
        }
      })
    },
    brightenKeyword(val,keyword){ // 显示搜索关键字为红色
      const Reg = new RegExp(keyword, 'i');
       if (val) {
          const res = val.replace(Reg, `<span style="color: #BB383E;">${keyword}</span>`);
          return res;
       }
    }
  }
}
</script>
<style scoped>

.ellipsis{
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-wrap: break-word;
  width: 66rem;
}
.personLi{
  height: 44px;
  line-height: 44px;
  padding-left: 16px;
  font-size: 14px;
  border-bottom: 1px solid #F4F4F4;
  
}
</style>